package org.builder.excel;

import org.annotation.ExcelColumn;
import org.apache.lucene.util.RamUsageEstimator;
import org.builder.excelutil.POIExcelExportHSFImp;
import org.util.DataUtil;
import org.util.MethodUtils;
import org.util.ScanExcelBeanTool;

/**
 * @author huangshen
 * @version 0
 * @description NormalExcelImp
 * @since 2022/1/30 20:54
 */
public class NormalExcelImp extends BaseExcelExportBuild {

    @Override
    public BaseExcelExportBuild prepared() {
        // 获取get方法集合
        this.getMethodList = new MethodUtils(exportBean).getGetMethodListByAnnotation(ExcelColumn.class);
        // 获取列名集合
        this.columnNameList = new ScanExcelBeanTool(exportBean).registerExcelColumnName();
        // 初始化导出工具
        this.exportUtil = new POIExcelExportHSFImp(excelTitle, getMethodList.size());

        return this;
    }

    @Override
    public BaseExcelExportBuild setRowData() {
        for (Object targetBean : beanList) {
            if (DataUtil.isRemainderZero(totalRowNum, maxRowNum)) {
                // 设置sheet名
                StringBuilder sheetName = new StringBuilder(excelTitle).append(totalRowNum / maxRowNum + 1);
                // 创建新Sheet
                exportUtil.createSheet(sheetName.toString());
            }
            for (int index = 0; index < getMethodList.size(); index++) {
                // 设置单元格参数
                exportUtil.setRowData(String.valueOf(MethodUtils.getValue(getMethodList.get(index), targetBean)), index);
            }
            totalRowNum++;
            System.out.println(totalRowNum);
        }
        return this;
    }
}
